﻿using Microsoft.Practices.Unity.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

using ERPStore.Extensions;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(ERPStore.Web.Start.UnityWebActivator), "Start")]

namespace ERPStore.Web.Start
{
	/// <summary>Provides the bootstrapping for integrating Unity with ASP.NET MVC.</summary>
	public static class UnityWebActivator
	{
		/// <summary>Integrates Unity when the application starts.</summary>
		public static void Start()
		{
			var container = GlobalConfiguration.Configuration.DependencyResolver.GetContainer();

			FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
			if (GlobalConfiguration.Configuration.ExecutingContext != ERPStore.Models.ExecutingContext.Test)
			{
				FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));
				var unityResolver = new UnityDependencyResolver(container);
				System.Web.Mvc.DependencyResolver.SetResolver(unityResolver);

				// TODO: Uncomment if you want to use PerRequestLifetimeManager
				Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
			}
			else
			{
				FilterProviders.Providers.Add(new IOC.UnityFilterProvider(container));
				var unityResolver = new IOC.UnityDependencyResolver(container);
				System.Web.Mvc.DependencyResolver.SetResolver(unityResolver);
				GlobalConfiguration.Configuration.DependencyResolver = unityResolver;
			}
		}
	}
}
